const Router = require('koa-router');
const tool = require("../../tools/tool");
const router = new Router();
const {db,  QueryTypes} = require("../../models/db");
// 新票据中的 右侧列表搜索
router.post('/bill/getInvoice', async ctx => {
    try {
        let user = ctx.user
        let tenantId = user.tenantId
        let body = ctx.request.body
        let formData = body.formData
        let billState = formData.billState
        let transType = tool.xe.toNumber(formData.transType)
        let beginDate = formData.beginDate
        let endDate = formData.endDate
        let buId = formData.buId

        let billNo = formData.billNo
        let pageSize = body.pageSize ? body.pageSize : 50
        let currentPage = body.currentPage ? body.currentPage : 1
        let offset = (currentPage - 1) * pageSize
        if(transType !== 190001) {
            let sqlstr = `select a.id,a.billNo,a.billState,a.billDate,a.buId,a.buName ,a.totalAmount,a.printNo,a.billStateName,a.salesId,a.salesName,a.orgin 
                from v_main a where a.tenantId=${tenantId} and a.transType=${transType} `
            if (billState) {
                if (billState === 'all') {
                    sqlstr += ` and a.billState in('accounted', 'draft') `
                }
                if (billState === 'accounted') {
                    sqlstr += ` and a.billState='accounted' `
                }
                if (billState === 'draft') {
                    sqlstr += ` and a.billState='draft' `
                }
            }
            if (beginDate) {
                sqlstr += ` and a.billDate>='${beginDate}' `
            }
            if (endDate) {
                sqlstr += ` and a.billDate<='${endDate}' `
            }
            if (buId) {
                sqlstr += ` and a.buId=${buId} `
            }
            if (billNo) {
                sqlstr += ` and a.billNo like '%${billNo}%' `
            }
            let sql = ` with bills as (${sqlstr}) `

            let totalsql = ` ${sql} select count(*) total from bills `

            let totalModel = await db.query(totalsql, { type: QueryTypes.SELECT, plain: true })
            let s1 = `
            ${sql} select * from bills order by billState desc,billDate desc offset ${offset} rows fetch next ${pageSize} rows only
            `
            let list = await db.query(s1, { type: QueryTypes.SELECT })
            list.map(row => {
                if(row.orgin === 'app') {
                    row.orgin = '手机'
                }
                else if(row.orgin === 'zd') {
                    row.orgin = '终端'
                }
                else {
                    row.orgin = ''
                }
            })
            return ctx.suc('', {
                total: totalModel.total,
                list: list
            })
        }
        else {
            let sqlstr = `select a.id,a.billNo,a.billState,a.billDate,a.buId,b.name buName , 
                    case when a.billState='draft' then N'草稿' when a.billState='accounted' then N'已审核' end billStateName 
                from card a left join contact b on a.buId=b.id where a.tenantId=${tenantId} and a.transType=${transType} `
            if (billState) {
                if (billState === 'all') {
                    sqlstr += ` and a.billState in('accounted', 'draft') `
                }
                if (billState === 'accounted') {
                    sqlstr += ` and a.billState='accounted' `
                }
                if (billState === 'draft') {
                    sqlstr += ` and a.billState='draft' `
                }
            }
            if (beginDate) {
                sqlstr += ` and a.billDate>='${beginDate}' `
            }
            if (endDate) {
                sqlstr += ` and a.billDate<='${endDate}' `
            }
            if (buId) {
                sqlstr += ` and a.buId=${buId} `
            }
            if (billNo) {
                sqlstr += ` and a.billNo like '%${billNo}%' `
            }
            let sql = `
                with bills as (${sqlstr}) 
            `

            let totalsql = `${sql } select count(*) total from bills `
            let totalModel = await db.query(totalsql, { type: QueryTypes.SELECT, plain: true })
            let s1 = `
             ${sql} select * from bills order by billState desc,billDate desc offset ${offset} rows fetch next ${pageSize} rows only
            `
            let list = await db.query(s1, { type: QueryTypes.SELECT })
            return ctx.suc('', {
                total: totalModel.total,
                list: list
            })
        }
    } catch (e) {
        return ctx.err(e);
    }
})

module.exports = router;